<?php
require_once('pworks/common/exception/SystemException.class.php');

class FileUtil{

    /**
     * Make a new directory, if needed, create all non-existent forefather directories.
     *
     * @param string $path
     */
    public static function newPath($path){
        if(is_file($path)){
            throw new SystemException(SystemException::OS_ERROR, "Can not create directory $path, it is an existent file");
        }

        if(is_dir($path)){
            return;
        }

        $parentPath = dirname($path);
        if(!is_dir($parentPath)){
            self::newPath($parentPath);
        }
        mkdir($path);
    }

    /**
     * Make a new empty file, if needed, create all non-existent forefather directories.
     *
     * @param string $path
     */
    public static function newFile($path){
        if(is_dir($path)){
            throw new SystemException(SystemException::OS_ERROR, "Can not create file $path, it is an existent directory");
        }

        if(is_file($path)){
            return;
        }

        $parentPath = dirname($path);
        if(!is_dir($parentPath)){
            self::newPath($parentPath);
        }
        
        $handle = fopen($path,'w');
        fflush($handle);
        fclose($handle);
    }


    /**
     * Delete subtree(all directories and files)
     *
     * @param string $path
     *
     */
    public static function deleteTree($path){
        
    }
}
